package io.flutter.plugins;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;
import com.example.freemusic.R;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;

public class FlutterOpenAppPlugin implements MethodChannel.MethodCallHandler {
    public static String CHANNEL = "com.jt.freemusic/flutter";

    static MethodChannel channel;
    private Activity activity;

    private FlutterOpenAppPlugin(Activity activity){
        this.activity = activity;
    }

    public static void registerWith(PluginRegistry.Registrar registrar) {
        channel = new MethodChannel(registrar.messenger(), CHANNEL);
        FlutterOpenAppPlugin instance = new FlutterOpenAppPlugin(registrar.activity());
        channel.setMethodCallHandler(instance);
    }

    @Override
    public void onMethodCall(MethodCall methodCall,  MethodChannel.Result result) {
        if (methodCall.method.equals("joinGroup")) {
            Intent intent = new Intent();
            intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3DbMFKxi0XycVuLxq5S5MPL4UHyzM9yw0h"));
            activity.startActivity(intent);
            result.success(true);
        } else if ( methodCall.method.equals("notification")){
            String imgsrc = methodCall.argument("img");
            String songName = methodCall.argument("songName");
            String singerName = methodCall.argument("singerName");
            _handleNotification(imgsrc, songName, singerName);
            result.success(true);
        } else {
            result.notImplemented();
        }
    }

    private void _handleNotification(String img, String songName, String singerName){

        int back = Color.BLACK;
        NotificationManager notificationManager = (NotificationManager) activity.getSystemService(activity.NOTIFICATION_SERVICE);

        RemoteViews remoteViews = new RemoteViews(activity.getPackageName(), R.layout.notification);
        remoteViews.setTextViewText(R.id.v_singer_name, singerName);
        remoteViews.setTextColor(R.id.v_singer_name, back);
        remoteViews.setTextViewText(R.id.v_song_name, songName);
        remoteViews.setTextColor(R.id.v_song_name, back);
//        remoteViews.setImageViewBitmap(R.id.i_song_img, getURLImage(img));
        remoteViews.setOnClickPendingIntent(R.id.b_play, PendingIntent.getBroadcast(activity, 10, play(), PendingIntent.FLAG_UPDATE_CURRENT));

        Notification notification = new NotificationCompat.Builder(activity, "notification")
                .setSmallIcon(R.mipmap.ic_launcher)
//                .setCustomBigContentView(remoteViews)
                .setContent(remoteViews)
                .build();
        notificationManager.notify(0, notification);

    }

    private Intent play(){
        Intent intent = new Intent(activity, NotificationReceiver.class);
        intent.setAction(NotificationReceiver.ACTION_PLAY);
        return intent;
    }

    private Bitmap getURLImage(String imgSrc){
        Bitmap bitmap = null;
        try {
            URL imgURL = new URL(imgSrc);
            HttpURLConnection connection = (HttpURLConnection) imgURL.openConnection();
            connection.setConnectTimeout(6000);
            connection.setDoInput(true);
            connection.setUseCaches(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
            return bitmap;
        }catch ( Exception e){
            System.out.println(e);
        }
        return bitmap;
    }
}